home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 205_01 / rs.c < prev    next >
Text File  |  1980-01-01  |  2KB  |  69 lines

  1. /*
  2. HEADER:                 CUG205.00;
  3. TITLE:                  Whitespace Remover;
  4. DATE:                   09/24/86;
  5. DESCRIPTION:
  6.   "Filter to remove whitespaces (SPACEs and TABs) from the ends of lines.";
  7. KEYWORDS:               Software tools, Text filters,whitespace remover,
  8.                         space, tabs;
  9. SYSTEM:                 MS-DOS;
  10. FILENAME:               RS.C;
  11. WARNINGS:
  12.   "The author claims copyrights and authorizes non-commercial use only.";
  13. AUTHORS:                 Michael M. Yokoyama;
  14. COMPILERS:              Microsoft;
  15. */
  16.  
  17. #include <stdio.h>
  18. #define MAXLINE 256 
  19. #define YES     1
  20. #define NO      0
  21.  
  22. main(argc,argv)
  23. int argc;                       /* Number of command line words   */
  24. char *argv[];                   /* Pointers to command line words */
  25. {
  26.   char *result, line[MAXLINE];          /* Current input line */
  27.  
  28.   if (argc != 1) {
  29.     fprintf(stderr,"rs:  remove space filter\n");
  30.     fprintf(stderr,"Error: extra characters after command\n");
  31.     exit(1);
  32.   }
  33.  
  34.   while ((result = gets(line)) != NULL) {
  35.     remove(line);
  36.     puts(line);
  37.   }
  38. }
  39.  
  40. /* Remove trailing blanks and tabs */
  41. remove(s)       
  42. char s[];
  43. {
  44.   int i;
  45.   int nl;                               /* Newline character flag        */
  46.  
  47.   i = 0;
  48.   while(s[i] != '\0')                   /* Find the end of char string s */
  49.     ++i;
  50.   --i;
  51.   if (s[i] == '\n') {
  52.     nl = YES;
  53.     --i;
  54.   }
  55.   else
  56.     nl = NO;
  57.   while ( i >= 0 && (s[i] == ' ' || s[i] == '\t' ) )
  58.     --i;
  59.   if (i >= 0) {
  60.     ++i;
  61.     if (nl == YES) {
  62.       s[i] = '\n';
  63.       ++i;
  64.     }
  65.     s[i] = '\0';
  66.   }
  67.   return(i);
  68. }
  69.